package com.example.chenduoyun.safe.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.chenduoyun.safe.R;
import com.example.chenduoyun.safe.utils.ConstantValue;
import com.example.chenduoyun.safe.utils.SpUtils;
import com.example.chenduoyun.safe.utils.ToastUtil;

/**
 * @author by created chen cloudy 2018/9/10 14:07
 **/

public class Setup3Activity extends BaseSetupActivity implements View.OnClickListener {
    private EditText et_phone_number;
    private Button btn_select_number;
    private final int PHONE_LENGTH = 11;
    private final int MY_PERMISSION_REQUEST_CODE1=10001;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup3);
        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.btn_pre).setOnClickListener(this);
        initUI();
    }

    @Override
    public void showNextPage() {
        judgeNextPage();
    }

    @Override
    public void showPrePage() {
        judgePerPage();
    }

    private void initUI() {
        et_phone_number = findViewById(R.id.et_phone_number);
        et_phone_number.setText(SpUtils.getString(this, ConstantValue.CONTACT_PHONE, ""));
        btn_select_number = findViewById(R.id.btn_select_number);
        btn_select_number.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                judgeNextPage();
                break;
            case R.id.btn_pre:
                judgePerPage();
                break;
            case R.id.btn_select_number:
                if(checkSelfPermissionAll( new String[]{Manifest.permission.READ_PHONE_STATE
                        , Manifest.permission.READ_CONTACTS})) {
                    Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
                    startActivityForResult(intent, 0);
                }else{
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE
                            , Manifest.permission.READ_CONTACTS}, MY_PERMISSION_REQUEST_CODE1);
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                }
                break;
            default:
                break;
        }
    }
    private boolean checkSelfPermissionAll(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                Log.i("cdy", "checkSelfPermissionAll: "+permission);
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==MY_PERMISSION_REQUEST_CODE1){
            boolean isPermission = true;
            for(int grant:grantResults){
                if(grant!=PackageManager.PERMISSION_GRANTED){
                    isPermission=false;
                    break;
                }
            }
            if(isPermission){
                Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
                startActivityForResult(intent, 0);
            }
        }


    }

    private void judgePerPage() {
        Intent intent = new Intent(getApplicationContext(), Setup2Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }

        private void judgeNextPage() {
        String phone = et_phone_number.getText().toString().trim();
        if (!TextUtils.isEmpty(phone) && phone.length() == PHONE_LENGTH) {
            SpUtils.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone);
            Intent intent = new Intent(getApplicationContext(), Setup4Activity.class);
            startActivity(intent);
            finish();
            overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
        } else {
            ToastUtil.show(this, "请输入正确的手机号");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            String phone = data.getStringExtra("phone");
            et_phone_number.setText(phone);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
